﻿using FastEF.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace FastEF.Util
{
    public class MapperUtil
    {
        public static string GetTableName<T>() where T : TModel
        {
            Type tp = typeof(T);
            return tp.Name;
        }
        public static  string GetKeyField<T>() where T : TModel
        {
            string keyfieldname = string.Empty;
            Type tp = typeof(T);
            PropertyInfo[] info = tp.GetProperties();
            foreach (PropertyInfo var in info)
            {
                //获取该属性上的标签集合
                Object[] attr = var.GetCustomAttributes(false);
                if (attr.Length > 0)
                {
                    //如果标签为key主键标签
                    if (attr[0].ToString() == typeof(System.ComponentModel.DataAnnotations.KeyAttribute).FullName)
                    {
                        keyfieldname = var.Name;
                    }
                }
            }
            if (string.IsNullOrEmpty(keyfieldname))
            {
                throw new Exception("数据表实体类未指定主键");
            }
            return keyfieldname;
        }
    }
}
